package com.study.bitimage;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Date;
import java.util.List;

/**
 * @author wanghuilong
 * @date 2020/12/27 12:23 上午
 */
@Slf4j
public class BitImageTest {

    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    public static void main(String[] args) throws ParseException {
        count(-2);
        count(-1);
        count(16);
        count(17);
        System.out.println(1 << -1);
        System.out.println(Integer.MIN_VALUE);

        BitSet bitSet = new BitSet();

        countDay("2020-12-01 00:00:00");

        countDay("2020-12-21 00:00:00");

        String str1 = "sdfasdqweqweqwqweqweqwe";

        int i = str1.hashCode();
        System.out.println(i);
        System.out.println(getHashCode(str1.toCharArray()));

        int i1 = "sdfsad".hashCode();
        System.out.println(i1);

        System.out.println(18596 % 365);

        System.out.println(Integer.MIN_VALUE * 31 * 31);

        System.out.println(Integer.MIN_VALUE * 31 + 10);

        int n = -907127267 * 31;
        System.out.println(n);

        int n1 = 907127267 * 31;
        System.out.println("n1:" + n1);

        String str = "11010001100001000111001001001111101";

        System.out.println(str);
        System.out.println("1110011110111000110110110000011");

        long n3 = 28120945277L;
        while (n3 > Integer.MAX_VALUE) {
            System.out.println("n3:" + n3);
            n3 = n3 >> 1;
        }
        System.out.println("n3:" + n3);

        System.out.println("n4:" + ((int) 28120945277L));

        System.out.println(str.substring(0, 10));

        System.out.println(Integer.MIN_VALUE);
        System.out.println(Integer.MAX_VALUE);

        // -907127267 * 31
        //
        // -907127267 原码:1(符号位)0110110000100011010100111100011
        // -907127267 反码:1(符号位)1001001111011100101011000011100
        // -907127267 补码:1(符号位)1001001111011100101011000011101
        // 31         原码:0(符号位)11111


        cheng(-907127267, 31);
        int intResult = -907127267 * 31;
        long longResult = -907127267L * 31L;
        System.out.println("int result:" + intResult);
        System.out.println("long result:" + longResult);


        System.out.println(Integer.toBinaryString(intResult));


        String s1 = Long.toBinaryString(longResult);
        System.out.println(Long.toBinaryString(longResult));
        System.out.println(s1.substring(s1.length() - 32 + 1));
        System.out.println(s1.substring(s1.length() - 32 + 1).length());
        System.out.println(s1.substring(s1.length() - 32));
        System.out.println(s1.substring(32).length());

        System.out.println(s1.length());

        n = -907127267;
        System.out.println(Integer.toBinaryString(n));

        String s = twoAdd("1101", "1111");
        System.out.println(s);


    }


    public static void cheng(int n1, int n2) {
        String n1Str = ten2two(n1);
        log.info("n1:{} 原码:{}", n1, n1Str);
        String inverseCodeStr = coverInverseCode(n1Str, n1);
        log.info("n1:{} 反码:{}", n1, inverseCodeStr);
        String complement = coverComplement(inverseCodeStr, n1);
        String result = twoCheng(complement, n2);
        System.out.println("cheng result: " + result);
        System.out.println("cheng result.size: " + result.length());

    }

    private static String twoCheng(String complement, int n2) {
        String result = complement;
        for (int i = 0; i < n2; i++) {
            result = twoAdd(result, complement);
        }
        return result;
    }

    /**
     * 反码转补码
     *
     * @param inverseCodeStr
     * @param n1
     * @return
     */
    private static String coverComplement(String inverseCodeStr, int n1) {
        if (n1 >= 0) {
            //正数的反码与原码相同，与反码相同
            return inverseCodeStr;
        }
        //负数的补码是在其反码的末尾加1
        return twoAdd(inverseCodeStr, "1");
    }


    private static String twoAdd(String complementStr1, String complementStr2) {
        int n = complementStr1.length();
        int min = complementStr1.length();
        if (complementStr2.length() > n) {
            n = complementStr2.length();
        } else {
            min = complementStr2.length();
        }

        List<Character> list = new ArrayList<>();
        char up = 0;
        char lastUp = 0;

        for (int i = 0; i < n; i++) {
            char c1 = getCharAt(complementStr1, i);
            char c2 = getCharAt(complementStr2, i);
            char c = '0';
            if (c1 == c2) {
                if (c1 == '1') {
                    up = 1;
                }
                c = '0';
            } else {
                if (c1 == 0) {
                    c = c2;
                }
                if (c2 == 0) {
                    c = c1;
                }
                if (c1 != 0 && c2 != 0) {
                    c = '1';
                }
            }

            if (lastUp == 1) {
                if (c == '1') {
                    up = 1;
                    c = '0';
                } else {
                    c = '1';
                }
            }

            lastUp = up;
            up = 0;

            list.add(c);
        }

//        for (int i = min; i < n; i++) {
//
//        }

        if (lastUp == 1) {
            list.add('1');
        }

        StringBuilder stringBuilder = new StringBuilder();
        for (int i = (list.size() - 1); i >= 0; i--) {
            stringBuilder.append(list.get(i));
        }
        return stringBuilder.toString();
    }

    private static char getCharAt(String str, int i) {
        int index = str.length() - i - 1;
        if (index < 0) {
            return 0;
        }
        return str.charAt(index);
    }

    public static String ten2two(int n1) {
        int temp = n1;
        int start = n1 >= 0 ? 0 : 1;
        StringBuilder stringBuilder = new StringBuilder("" + start);
        for (int s = 31; s >= 0; s--) {
            stringBuilder.append(temp >>> s & 1);
        }
        return stringBuilder.toString();
    }

    /**
     * 原码转反码
     *
     * @param n1Str
     * @param n1
     * @return
     */
    private static String coverInverseCode(String n1Str, int n1) {
        //正数的反码和原码相同
        if (n1 >= 0) {
            return n1Str;
        }
        int start = (n1 >= 0 ? 0 : 1);
        //负数，除符号位外，对原码按位取反
        StringBuilder stringBuilder = new StringBuilder("" + start);
        for (int i = 1; i < n1Str.length(); i++) {
            char c = n1Str.charAt(i);
            if ('1' == c) {
                stringBuilder.append('0');
            } else {
                stringBuilder.append('1');
            }
        }

        return stringBuilder.toString();
    }


    public static int getHashCode(char val[]) {
        int h = 0;

        for (int i = 0; i < val.length; i++) {
            h = 31 * h + val[i];
            System.out.println("getHashCode:" + h + " v:" + ((int) val[i]));
        }

        return h;
    }

    private static void countDay(String dateStr) throws ParseException {
        Date date = simpleDateFormat.parse(dateStr);
        long daySize = date.getTime() / 24L / 3600000L;
        System.out.println("dateStr:" + dateStr + ",daySize:" + daySize);
    }

    private static void count(int i) {
        System.out.println("i:" + i);
        int index = i % 16 == 0 ? i / 16 : i / 16;
        int temp = i - index * 16;
        System.out.println("temp:" + temp);
        int splitindex = 1 << temp;

        System.out.println("index:" + index + " splitindex:" + splitindex);
        System.out.println("________________");
    }


}
